package guava.study.lyh.other;

import com.google.common.base.*;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.Validate;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 作者 yaohua.liu
 * 日期 2014-08-04 18:32
 * 包名 com.qunar.fresh.lyh
 * 说明 ...
 */
public class Guava_6_Strings_字符串处理_分割_连接_填充 {
    @Test
    public void testJoiner() {
        Joiner joiner = Joiner.on(";")/*.skipNulls()*/.useForNull("null");
        Preconditions.checkState("Harry;Ron;Hermione".equals(joiner.join("Harry", null, "Ron", "Hermione")),
                "%s!=%s",
                "Harry;Ron;Hermione",
                joiner.join("Harry", null, "Ron", "Hermione"));

        Preconditions.checkState(Joiner.on(",").join(Arrays.asList(1, 5, 7)).equals("1,5,7"));
    }

    @Test
    public void testSplitter() {
        Splitter splitter = Splitter
                .on(',')
                        //.onPattern("\r?\n")//按正则表达式拆分
                        //.trimResults(CharMatcher)//给定匹配器，移除结果字符串的前导匹配字符和尾部匹配字符
                .trimResults()//移除结果字符串的前导空白和尾部空白
                .omitEmptyStrings();//从结果中自动忽略空字符串

        Iterable iterable = splitter.split(",,foo,bar,,   qqux,,");
        ArrayList result= Lists.newArrayList(iterable);
        System.out.println(result);
    }


    @Test
    public void testCaseFormat(){
        String str="CONSTANT_NAME";
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str)); // returns "constantName"
    }

    @Test
    public void testValidate() {
        Validate.matchesPattern("2013-11-241", "\\d{4}-\\d{2}-\\d{2}", "输入的日期格式不正确！");
    }

    @Test
    public void testCharsets() {
        byte[] bytes;
        String string = new String("aa");
        try {
            bytes = string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // how can this possibly happen?
            throw new AssertionError(e);
        }

        bytes=string.getBytes(Charsets.UTF_8);
    }

}
